package web; // erro em todos as class... arrumar o caminho do JRE --> Properteis --> Java Build Patch --> Libraries --> "JRE System Library"

import gerenciador.Empresa;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import conexao.ConnectionPool;
import dao.EmpresaDAOBD;

@WebServlet(urlPatterns="/buscabd")
public class BuscaEmpresaBD extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

		PrintWriter writer = resp.getWriter();

		writer.println("<html><body>");
		writer.println("<br>Resultado da Busca: <br/>");

		String filtro = req.getParameter("filtro");
		
		try( Connection con = new ConnectionPool().getConnection() ){
	
			writer.println("<lu>");
			
			Collection<Empresa> empresas = new EmpresaDAOBD(con).buscaPorSimilaridade(filtro);
			
			for (Empresa empresa : empresas) {				
				writer.println("<li>Id: "+ empresa.getId()+"  - Nome: "+empresa.getNome()+"</li>");
			}

     		writer.println("</lu> </body></html>");
		
		}catch (SQLException e) {
		    writer.println("<html><body> Erro: " + e + "</html></body>");
		}	
	}	
}
